package com.axjy.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * Created by asus on 2018/1/13.
 */
public class Tools {

    public static String hanzi2pinyin(char hanzi){
        HanyuPinyinOutputFormat format=new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        try {
            String[] arr=PinyinHelper.toHanyuPinyinStringArray(hanzi,format);
            for(String c:arr){
                System.out.println(c);
            }
            return arr[0];
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return "";
    }

    public static String hanzi2pinyin(String hanzi){
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<hanzi.length();i++){
            char ch=hanzi.charAt(i);
            String str=hanzi2pinyin(ch);
            stringBuilder.append(str);
        }
        return stringBuilder.toString();
    }

}
